<template>
  <div class="upload">
    <el-upload
      class="avatar-uploader"
      action="#"
      :before-upload="beforeAvatarUpload"
      :show-file-list="false"
      :http-request="httpRequest"
    >
      <img
        v-if="imageUrl"
        v-imgErr="defaultImg"
        :src="imageUrl"
        class="avatar"
        @click.stop="dialogVisible = true"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 删除图片 -->
    <div v-if="imageUrl" class="icon" @click="$emit('input', '')">
      <i class="el-icon-delete-solid" />
    </div>
    <!-- 图片预览 -->
    <el-dialog title="图片预览" :visible.sync="dialogVisible">
      <div style="text-align: center">
        <img class="img" :src="imageUrl" alt="">
      </div>
    </el-dialog>
    <el-progress v-if="isPro" :percentage="percentage" />
  </div>
</template>

<script>
import Cos from 'cos-js-sdk-v5'
const cos = new Cos({
  SecretId: 'AKIDoRnu2uVpsVvAAuOcbKn0yg8JDrFNGwMW', // 身份识别Id
  SecretKey: 'wd4GCtKVP2OTLwHL8gz3rPp1xc07xWop' // 身份密钥
})
export default {
  name: 'Elupload',
  model: {
    prop: 'imageUrl'
  },
  props: {
    imageUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: false,
      isPro: false,
      percentage: 0,
      defaultImg: require('@/assets/common/bigUserHeader.png')
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/png' || 'image/jepg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    httpRequest(res) {
      // 初始化数据
      this.isPro = true
      this.percentage = 0
      console.log(res)
      cos.putObject(
        {
          Bucket: 'author-1308460197', // 存储桶名称
          Region: 'ap-nanjing', // 存储桶所在地域
          Key: res.file.uid + '', // 唯一标识,必须是字符串
          StorageClass: 'STANDARD', // 固定值
          Body: res.file, // 文件对象
          onProgress: (progressData) => {
            // 进度条
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              this.isPro = false
              // 达成条件隐藏进度条
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 上传成功或者失败的回调
          console.log(err || data)
          // this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.upload {
  position: relative;
  width: 178px;
  &:hover {
    .icon {
      display: block;
    }
  }
  .icon {
    position: absolute;
    top: 5px;
    right: 5px;
    display: none;
    cursor: pointer;
  }
}
</style>
